home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / src / RLD_source.lha / RLD_source / libinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-22  |  6.4 KB  |  254 lines

  1. /*
  2.   $Id: libinit.c,v 1.2 1997/10/21 22:35:08 wegge Stab wegge $
  3.  
  4.   $Log: libinit.c,v $
  5.   Revision 1.2  1997/10/21 22:35:08  wegge
  6.   Snapshot inden upload af 2.13 i source og binær form
  7.  
  8.   Revision 1.1  1997/10/21 03:49:58  wegge
  9.   Initial revision
  10.  
  11.  */
  12.  
  13. /* Definitions that are private to libint. */
  14. #include "libinit_priv.h"
  15. /* Library wide definitions. */
  16. #include "rexx_gls.h"
  17.  
  18. /* Exit safely, is the library should happen to be executed. */
  19.  
  20. LONG FailOnRun(VOID)
  21. {
  22.   return (-1);
  23. }
  24.  
  25. /*
  26.  * The resident structure. 
  27.  */
  28.  
  29. static const struct Resident RomTag =
  30. {
  31.   /* The magic cookie and pointer, that identifies this as a Resident
  32.      structure.  */
  33.   RTC_MATCHWORD,
  34.   (struct Resident *) &RomTag,
  35.  
  36.   /* InitResident need a place to continue search, after looking at
  37.      this structure. The rules say that we should point to a place
  38.      after this Resident, but within the same section. */
  39.   (APTR) ((&RomTag) + 1),    
  40.  
  41.   /* rt_Init has valid content  */
  42.   RTF_AUTOINIT,
  43.  
  44.   /* Version, defined in libinit_priv.h */
  45.   REXXGLS_VER,        
  46.  
  47.   /* This module is a library. */
  48.   NT_LIBRARY,
  49.  
  50.   /* We don't need any special priority. */
  51.   0,                /* No special priority needed. */
  52.  
  53.   /* This string *must* match the filename of the library. Otherwise
  54.      Exec will (if we are lucky) load a new copy of this library for
  55.      each OpenLibrary(). */
  56.   (BYTE *) LibName,    
  57.  
  58.   /* This string is what Version and other similar tools will look
  59.      for. */
  60.   (BYTE *) LibIdString,
  61.  
  62.   /* Data for MakeLibrary. */
  63.   (APTR) & InitTab    
  64. };
  65.  
  66. /* InitResident() uses this table for initialization purposes. */
  67.  
  68. const APTR InitTab[4] =
  69. {
  70.   /* The size of the entire library base. */
  71.   (APTR) sizeof(struct RexxGLSBase),
  72.  
  73.   /* The table of entrypoints to the library. */
  74.   (APTR) &__rgls_functable__[0],
  75.   
  76.   /* We dont use InitStructs. */
  77.   0L,
  78.  
  79.   /* Instead we do all initializing on our own. */
  80.   (APTR) LibInit
  81. };
  82.  
  83. /* Library Name & Version strings.  */
  84.  
  85. const BYTE LibName[] = REXXGLS_NAME;
  86. const BYTE LibIdString[] = REXXGLS_VERSTAG;
  87.  
  88. /* Library init function called by MakeLibrary. */
  89.  
  90. struct Library *LibInit(APTR SegList __asm("a0"),
  91.             struct RexxGLSBase *RglsBase __asm("d0"),
  92.             struct ExecBase *ExecBase __asm("a6"))
  93. {
  94.  
  95.   KPRINTF_HERE;
  96.   
  97.   /* We keep the SegList for the time we leave. */
  98.   RglsBase->rgls_seglist = SegList;
  99.   RglsBase->rgls_SYSBase = ExecBase;
  100.  
  101.   /*
  102.    * Initialize our own Library base.
  103.    */
  104.  
  105.   RglsBase->rgls_lib.lib_Node.ln_Type = NT_LIBRARY;
  106.   RglsBase->rgls_lib.lib_Node.ln_Name = (UBYTE *) LibName;
  107.   RglsBase->rgls_lib.lib_Flags = (LIBF_CHANGED | LIBF_SUMUSED);
  108.   RglsBase->rgls_lib.lib_Version = (UWORD) REXXGLS_VER;
  109.   RglsBase->rgls_lib.lib_Revision = (UWORD) REXXGLS_REV;
  110.   RglsBase->rgls_lib.lib_IdString = (APTR) LibIdString;
  111.   InitSemaphore(&RglsBase->RexxGLS_Sem);
  112.   RglsBase->CookieCount=0;
  113.  
  114.   /* We are running in a forbidden state, so we can't open the needed
  115.      libraries. Instead we NULL out the various bases, and open them
  116.      when needed. */
  117.  
  118.   RglsBase->rgls_DOSBase = NULL;
  119.   RglsBase->rgls_RexxSysBase = NULL;
  120.   RglsBase->rgls_UtilityBase = NULL;
  121.   RglsBase->rgls_LocaleBase = NULL;
  122.  
  123.   return ((struct Library *) RglsBase);
  124.  
  125. }
  126.  
  127. struct Library *LibOpen(struct  RexxGLSBase *RglsBase __asm("a6"))
  128. {
  129.  
  130. /* Each time this library is OpenLibrary()'d, this function will be
  131.    called. As for LibInit(), we are forbidden, so we limit the actions
  132.    to updating the OpenCount and clearing the delayed expunge flag. */
  133.   KPRINTF_HERE;
  134.  
  135.   RglsBase->rgls_lib.lib_Flags &= ~LIBF_DELEXP;
  136.   RglsBase->rgls_lib.lib_OpenCnt++;
  137.  
  138.   return ((struct Library *) RglsBase);
  139. }
  140.  
  141. /*
  142.  * Each call of CloseLibrary() will result in a call of this function.
  143.  */
  144.  
  145. APTR LibClose(struct  RexxGLSBase *RglsBase __asm("a6"))
  146. {
  147.  
  148. /* Each time this library is CloseLibrary()'d, this function will be
  149.    called. As for LibInit(), we are forbidden, so we limit the actions
  150.    to updating the OpenCount, and removing ourselves if needed. */
  151.  
  152.   /* We return this to indicate whether or not we are gone. */
  153.   APTR SegList = 0;
  154.   KPRINTF_HERE;
  155.  
  156.   RglsBase->rgls_lib.lib_OpenCnt--;
  157.   
  158.   /* If usage count hits zero, and the delayed expunge flag is set, we
  159.      should unload the library if possible. Note that this might not
  160.      always be the case due to ARexx habit of opening and closing
  161.      libraries at random. */
  162.  
  163.   if ((RglsBase->rgls_lib.lib_OpenCnt == 0) &&
  164.       (RglsBase->rgls_lib.lib_Flags & LIBF_DELEXP))
  165.     SegList = LibExpunge(RglsBase);
  166.  
  167.   /* Return seglist if LibExpunge unloaded us. Otherwise return NULL. */
  168.  
  169.   return (SegList);
  170. }
  171.  
  172. /* Unload RglsBase if possible. */
  173.  
  174. APTR LibExpunge(struct RexxGLSBase *RglsBase __asm("a6"))
  175. {
  176.  
  177. /* Each time the memory allocator need space, this function will be
  178.    called. If we can, we unload ourselves, otherwise Exec will have to
  179.    manage without our memory. */
  180.  
  181.   APTR SegList = 0;
  182.   KPRINTF_HERE;
  183.  
  184.   /* Set the delayed expunge flag. */
  185.  
  186.   RglsBase->rgls_lib.lib_Flags |= LIBF_DELEXP;
  187.  
  188.   /* If OpenCount is zero, we might be able to unload, but only if
  189.      there are no opened locales. */
  190.  
  191.   ObtainSemaphore(&RglsBase->RexxGLS_Sem);
  192.   
  193.   if ((RglsBase->rgls_lib.lib_OpenCnt == 0) &&
  194.       (RglsBase->CookieCount == 0))
  195.     {
  196.       ULONG NegSize;
  197.  
  198.       /* Return the seglist, so UnLoadSeg() has something to work
  199.      on. */
  200.  
  201.       SegList = RglsBase->rgls_seglist;
  202.  
  203.  
  204.       /* Remove RglsBase from SysBase->LibList. */
  205.  
  206.       Remove((struct Node *) RglsBase);
  207.  
  208.       /* Close any open libraries. */
  209.  
  210.       if (RglsBase->rgls_DOSBase != NULL)
  211.     CloseLibrary((struct Library *)RglsBase->rgls_DOSBase);
  212.       
  213.       if (RglsBase->rgls_RexxSysBase != NULL)
  214.     CloseLibrary((struct Library *)RglsBase->rgls_RexxSysBase);
  215.       
  216.       if (RglsBase->rgls_UtilityBase != NULL)
  217.     CloseLibrary((struct Library *)RglsBase->rgls_UtilityBase);
  218.       
  219.       if (RglsBase->rgls_LocaleBase != NULL)
  220.     CloseLibrary((struct Library *)RglsBase->rgls_LocaleBase);
  221.       
  222.       /* Free the Library base and vector table. */
  223.  
  224.       NegSize = RglsBase->rgls_lib.lib_NegSize;
  225.       FreeMem((APTR) ((UBYTE *) RglsBase - (UBYTE *) NegSize), NegSize +
  226.           RglsBase->rgls_lib.lib_PosSize);
  227.     }
  228.   ReleaseSemaphore(&RglsBase->RexxGLS_Sem);
  229.  
  230.   return SegList;
  231. }
  232.  
  233. /* This function is reserved for future compatibility. */
  234.  
  235. APTR LibExtFunc(struct  RexxGLSBase *RglsBase __asm("a6"))
  236. {
  237.   KPRINTF_HERE;
  238.  
  239.   /* We play by the rules, although it's tempting to return 42. */
  240.   return (0);
  241. }
  242.  
  243. /* List of functions in the library. */
  244.  
  245. const APTR __rgls_functable__[] =
  246. {
  247.   LibOpen,
  248.   LibClose,
  249.   LibExpunge,
  250.   LibExtFunc,
  251.   ArexxMatchPoint,
  252.   (APTR) - 1L
  253. };
  254.